﻿Imports System.Xml.Serialization
Imports System.IO

''' <summary>
''' Manages Rules
''' </summary>
''' <remarks>
''' This is the main class for XML Serilization.
''' </remarks>
Public Class RuleManager
	'I'm not quite sure why the hell I wrote this class, I could've just marked the "Rule" class as Serializable...


	''' <summary>
	''' Rules
	''' </summary>
	Public Rules As List(Of Rule)

	''' <summary>
	''' Initializes a new instance of the Rules class.
	''' </summary>
	Public Sub New()
	End Sub


	''' <summary>
	''' Saves to an XML file.
	''' </summary>
	''' <param name="rules">The rules.</param>
	''' <param name="file">The file.</param>
	Public Shared Sub SaveRules(ByVal rules As RuleManager, ByVal file As String)
		Dim seril As New XmlSerializer(GetType(RuleManager))
		Dim writer As New StreamWriter(file)
		seril.Serialize(writer, rules)
		writer.Close()
	End Sub

	''' <summary>
	''' Loads rules from an XML file.
	''' </summary>
	''' <param name="rulesFile">The rules file.</param>
	''' <returns></returns>
	Public Shared Function LoadRules(ByVal rulesFile As String) As RuleManager

		Dim rulesLoader As New XmlSerializer(GetType(RuleManager))
		Dim loadedManager As RuleManager

		Using xmlStreamReader As New StreamReader(rulesFile)
			loadedManager = DirectCast(rulesLoader.Deserialize(xmlStreamReader), RuleManager)
		End Using

		Return loadedManager
	End Function

End Class


''' <summary>
''' Process Rules
''' </summary>
Public Structure Rule
	Public Run As String
	Public Extension As String
	Public Name As String

	Public SendKeys As String
	Public KeyWait As Integer

	Public RuleWait As Integer

	''' <summary>
	''' Initializes a new instance of the Rule structure.
	''' </summary>
	''' <param name="name"></param>
	''' <param name="run"></param>
	''' <param name="extension"></param>
	Public Sub New(ByVal name As String, ByVal run As String, ByVal extension As String)
		Me.Run = run
		Me.Extension = extension
		Me.Name = name
	End Sub

	''' <summary>
	''' Initializes a new instance of the Rule structure.
	''' </summary>
	''' <param name="run"></param>
	''' <param name="extension"></param>
	Public Sub New(ByVal run As String, ByVal extension As String)
		Me.Run = run
		Me.Extension = extension
	End Sub
End Structure



